package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.*;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.*;
import com.tbit.uqbike.client.pojo.newEntity.AdvertConfig;
import com.tbit.uqbike.client.service.*;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.DateTimeUtil;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 区域广告
 *
 * @author Leon
 * 2019年12月26日 下午3:42:21
 */
@Controller
@RequestMapping("/adAccount")
public class AdAccountController {
    @Autowired
    private AdAccountService adAccountService;
    @Autowired
    private GeoService geoService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private WeixinPayConfigService weixinPayConfigService;
    @Autowired
    private AccountService accountService;

    /**
     * 获取当前广告
     *
     * @param accountId 帐户id
     * @param lo        经度
     * @param la        纬度
     * @param mapType   地图类型
     * @param adType    广告类型
     * @param response  响应
     * @ignoreParams request
     */
    @RequestMapping("/getByLocation")
    public void getByLocation(Integer accountId, Double lo, Double la, Integer mapType, Integer adType, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            Assert.notNull(accountId, "-10001", "品牌id不能为空");
            Assert.notNull(lo, "-2001", "经度不能为空");
            Assert.notNull(la, "-2002", "纬度不能为空");

            if (mapType == null) {
                mapType = MapConstant.MAP_GOOGLE;
            }

            /*校准经纬度为原始经纬度*/
            Point point = new Point(lo, la);
            point = calibrateService.calibrate(point, mapType, SysConstant.ORIGINAL);

            Integer agentId = geoService.getBelongAccount(accountId, lo, la, mapType);

            List<AdAccount> adAccounts = new ArrayList<AdAccount>();

            if (agentId != null) {
                adAccounts.addAll(adAccountService.getValid(agentId, adType, DateTimeUtil.getNowTime()));
            }

            map.put(ResponseCode.DATA, adAccounts);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

    /**
     * 获取所有广告配置
     *
     * @param accountId   品牌id
     * @param adAccountId 区域id
     * @param request
     * @param response
     */
    @RequestMapping("/getByAccountId")
    public void getByAccountId(Integer accountId, Integer adAccountId, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            Assert.notNull(accountId, "-10001", "品牌id不能为空");

            List<AdvertConfig> adAccounts = new ArrayList<>();
            /**先查区域，未配置用品牌配置*/
            if (adAccountId != null) {
                adAccounts = adAccountService.getByAccountId(adAccountId);
                //获取品牌id
                Account account = accountService.getByAdAccountId(adAccountId);
                if (account != null) {
                    adAccounts = getAdvertConfigs(account.getAccountId(), adAccounts);
                }
            }
            if (adAccounts.isEmpty()) {
                adAccounts = adAccountService.getByAccountId(accountId);
                adAccounts = getAdvertConfigs(accountId, adAccounts);
            }
            map.put(ResponseCode.DATA, adAccounts);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

    private List<AdvertConfig> getAdvertConfigs(Integer accountId, List<AdvertConfig> adAccounts) {
        BrandRemoteConfig brandRemoteConfig = cacheService.getBrandRemoteConfig(accountId, BrandConstant.ADVERT_FLAG);
        if (brandRemoteConfig != null && "1".equals(brandRemoteConfig.getParamKV())) {
            //查询缓存开关
            WeixinPayConfig wxPayConfig = weixinPayConfigService.getByAccountId(accountId, 2);
            if (wxPayConfig != null && redisService.get(RedisConstant.ADVERT_REDIS_KEY + wxPayConfig.getAppId()) == null) {
                adAccounts = new ArrayList<>();
            }
        }
        return adAccounts;
    }


}